package com.kitmobile.model;

import java.util.HashMap;

import android.content.ContentValues;
import android.database.Cursor;

import com.kitmobile.data.KITDatabase;
import com.kitmobile.data.KITDatabase.KITObject;

public class KITContact extends KITObject {

	private String contactID;
	private Frequency frequency;
	private long nextReminderDate;
	
	public static final long REMINDER_UNSET = Integer.MIN_VALUE;
	
	public KITContact() {
		super();
		invalidate();
	}
	
	public KITContact(String contactID) {
		super();
		invalidate();
		this.contactID = contactID;
		
	}
	
	public KITContact(String contactID, Frequency frequency, long nextReminderDate) {
		super();
		invalidate();
		this.contactID = contactID;
		this.frequency = frequency;
		this.nextReminderDate = nextReminderDate;
	}
	
	@Override
	public void invalidate() {
		super.invalidate();
		this.contactID = "";
		this.frequency = Frequency.none;
		this.nextReminderDate = REMINDER_UNSET;
	}
	
	public String getContactID() {
		return contactID;
	}
	public void setContactID(String contactID) {
		this.contactID = contactID;
	}
	
	public Frequency getFrequency() {
		return frequency;
	}

	public void setFrequency(Frequency frequency) {
		this.frequency = frequency;
	}

	public long getNextReminderDate() {
		return nextReminderDate;
	}

	public void setNextReminderDate(long nextReminderDate) {
		this.nextReminderDate = nextReminderDate;
	}

	@Override
	public String getNullableColumn() {
		return TableData.FREQUENCY;
	}

	@Override
	public HashMap<String, String> getProjectionMap() {
		return TableData.sProjectionMap;
	}

	@Override
	public String getTableName() {
		return TableData.TABLE_NAME;
	}

	@Override
	public void toContentValues(ContentValues values) {
		values.clear();

		values.put(TableData.CONTACT_ID, contactID);
		values.put(TableData.FREQUENCY, frequency.ordinal());
		values.put(TableData.NEXT_REMINDER_DATE, nextReminderDate);
	}
	
	@Override
	public void fromCursor(Cursor cursor) {
		super.fromCursor(cursor);

		contactID = cursor.getString(TableData.CONTACT_ID_COL);
		frequency = Frequency.values()[cursor.getInt(TableData.FREQUENCY_COL)];
		nextReminderDate = cursor.getLong(TableData.NEXT_REMINDER_DATE_COL);
	}
	
	public static enum Frequency {
		none,
		daily,
		weekly,
		monthly,
		yearly;
	}
	
	public static class TableData implements KITDatabase.TableData {
		public static final String TABLE_NAME = "kitContact";

		public static final String CONTACT_ID = "contactId";
		public static final int CONTACT_ID_COL = 1;

		public static final String FREQUENCY = "frequencyId";
		public static final int FREQUENCY_COL = 2;

		public static final String NEXT_REMINDER_DATE = "nextReminderDate";
		public static final int NEXT_REMINDER_DATE_COL = 3;
		
		public static final String CREATE_TABLE_STATEMENT = 
			"CREATE TABLE " + TABLE_NAME + " (" +
				_ID + " INTEGER PRIMARY KEY, " +
				CONTACT_ID + " INTEGER, " +
				FREQUENCY + " INTEGER, " +
				NEXT_REMINDER_DATE + " INTEGER);";
		
		public static final String DEFAULT_SORT_ORDER = "DESC";
		
		public static final HashMap<String, String> sProjectionMap;
		static {
			sProjectionMap = new HashMap<String, String>();
			sProjectionMap.put(_ID, _ID);
			sProjectionMap.put(CONTACT_ID, CONTACT_ID);
			sProjectionMap.put(FREQUENCY, FREQUENCY);
			sProjectionMap.put(NEXT_REMINDER_DATE, NEXT_REMINDER_DATE);
		}
	}
}
